package com.feng.study.demo.juc;

import com.feng.study.value.Student;
import com.feng.study.value.User;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class SyncDemo {
    public static void main(String[] args) throws InterruptedException {
        SyncTest syncTest = new SyncTest();

        new Thread(()-> {
            try {
                syncTest.deal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(()-> {
            try {
                syncTest.test();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();

//        syncTest.dealNum(10);
//        syncTest.dealNum02(3);
//        syncTest.deal();
//        syncTest.test();
        System.out.println("结束——————————————————");
    }
}

class SyncTest {
    private Integer num;
    private Student student=new Student().setAge(11).setName("xiaohong");
    private User user=new User().setAge(22).setName("xiaoming");

    synchronized public void deal() throws InterruptedException {
        System.out.println("deal--11111");
        TimeUnit.SECONDS.sleep(5);
        System.out.println("deal结束----");
    }

    synchronized public void test() {
        System.out.println("test--22222");
    }

    public void dealNum(int num) throws InterruptedException {
        int a = 10;

        synchronized (this) {
            for (int i = 0; i < num; i++) {
                System.out.println(i + "----"+a);
            }
        }
        TimeUnit.SECONDS.sleep(3);
        System.out.println("锁代码块，锁细粒度");
    }

    public void dealNum01(int num) throws InterruptedException {
        int a = 10;

        synchronized (student) {
            for (int i = 0; i < num; i++) {
                System.out.println(i + "----"+student);
            }
        }
        TimeUnit.SECONDS.sleep(3);
        System.out.println("锁代码块，锁细粒度");
    }

    public void dealNum02(int num) throws InterruptedException {
        synchronized (user) {
            for (int i = 0; i < num; i++) {
                System.out.println(i + "----"+user);
            }
        }
        TimeUnit.SECONDS.sleep(3);
        System.out.println("锁代码块，锁细粒度");
    }
}
